package com.example.home.view;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.common.component.MyToast;
import com.example.common.gamedetail.GameDetailActivity;
import com.example.common.model.GameApiGameResp;
import com.example.home.IStartGameDetailActivity;
import com.example.home.LeaderboardRAdapter;
import com.example.home.R;
import com.example.home.viewmodel.HomeLeaderboardViewModel;
import com.example.home.viewmodel.HomeViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class HomeLeaderboardFragment extends Fragment {

    private static final String TAG = "HomeLeaderboardFragment";
    private HomeLeaderboardViewModel viewModel;
    private RecyclerView recyclerView;

    public static HomeLeaderboardFragment newInstance() {
        return new HomeLeaderboardFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_leaderboard_fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView=view.findViewById(R.id.home_leaderboard_recyclerview);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = new ViewModelProvider(requireActivity()).get(HomeLeaderboardViewModel.class);

        // TODO: Use the ViewModel
        //创建adapter，传递viewmodel，为recyclerview设置layout和adapter
        LeaderboardRAdapter adapter=new LeaderboardRAdapter(viewModel);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
        recyclerView.setAdapter(adapter);

        //绑定路由的点击事件
        adapter.iStartGameDetailActivity = new IStartGameDetailActivity() {
            @Override
            public void openById(Integer id) {
                Intent intent = new Intent(requireActivity(), GameDetailActivity.class);
                intent.putExtra("ID", id);
                startActivity(intent);
            }
        };

        //解决刷新闪烁问题
        ((SimpleItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

        //监听提示消息
        viewModel.getRemainMsg().observe(requireActivity(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MyToast.showMyToast(requireActivity(),s,3);
            }
        });

        //adapter加载监听
        viewModel.getIsAdapterLoad().observe(requireActivity(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    //全部加载
                    adapter.notifyDataSetChanged();
                }
            }
        });

        //游戏数据更新监听
        viewModel.getIsGamesLoad().observe(requireActivity(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    //游戏数据更新
                    viewModel.loadGamesByPN();
                }
            }
        });
    }

}